package pt.fct.mpc.lastfm.album;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import pt.fct.mpc.lastfm.R;
import pt.fct.mpc.lastfm.database.LastfmDatabaseFavourites;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TabHost;
import android.widget.Toast;

public class AlbumActivity extends TabActivity {

	private static final String TAG = "Last.fm Album Activity";
	AlbumData albumDataSet = new AlbumData();
	String album_artist = null;
	String album_name = null;
	boolean gotAlbum = false;
	boolean gotTracks = false;

	ArrayAdapter<String> tracksArrayAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// Reads the info sent by the intent
		Intent the_intent_that_gave_life_to_everything = getIntent();
		Bundle intent_data = the_intent_that_gave_life_to_everything
				.getExtras();

		if (intent_data == null) {
			Log.e(TAG, "No extra data");
			finish();
		}

		String data_album_name = intent_data.getString("name");
		String data_artist_name = intent_data.getString("artist");

		albumDataSet.setArtist(data_artist_name);
		albumDataSet.setName(data_album_name);
		
		if ((data_album_name == null) || (data_artist_name == null)) {
			Log.e(TAG, "Null values from bundle: album name = "
					+ data_album_name + "; artist name = " + data_artist_name);
			finish();
		}

		try {
			album_artist = URLEncoder.encode(data_artist_name, "UTF-8");
			album_name = URLEncoder.encode(data_album_name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException("encoding problems");
		}

		// Creates the tabs and its content
		final TabHost tabHost = getTabHost();
		
		Bundle TabsData = new Bundle();
		TabsData.putSerializable("albumserial", albumDataSet);

		Intent AlbumInfoActivityIntent = new Intent(this,
				AlbumInfoActivity.class);
		AlbumInfoActivityIntent.putExtras(TabsData);

		Intent AlbumTracksActivityIntent = new Intent(this,
				AlbumTracksActivity.class);
		AlbumTracksActivityIntent.putExtras(TabsData);

		tabHost.addTab(tabHost.newTabSpec("Album").setIndicator("Album")
				.setContent(AlbumInfoActivityIntent));
		tabHost.addTab(tabHost.newTabSpec("Tracks").setIndicator("Tracks")
				.setContent(AlbumTracksActivityIntent));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.album_options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		LastfmDatabaseFavourites db_favourites = new LastfmDatabaseFavourites(
				this);

		switch (item.getItemId()) {
		case R.id.album_options_menu_add_favourite:

			db_favourites.addAlbum(albumDataSet.getArtist(), albumDataSet
					.getName());
			Toast.makeText(this, "Added to Favourites", Toast.LENGTH_SHORT)
					.show();
			break;
		}

		return true;
	}
}